package com.zm.admin.mq;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.zm.admin.config.RabbitConfig;
import com.zm.admin.ws.NioWebSocketChannelPool;
import com.zm.admin.ws.NioWebSocketHandler;
import com.zm.admin.ws.WsBean;
import com.zm.admin.zmeng.domain.ZmNotice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/***
 *
 * @Auther: MuYun
 * @Date: 2024/4/22 17:21
 * @Description：公告消费者
 *
 */
@Component
@Slf4j
public class NoticeConsumer {

    @Resource
    private NioWebSocketChannelPool socketChannelPool;


    /**
     * 监听公告队列并消费
     * @param message
     * @param channel
     * @throws Exception
     */
    @RabbitListener(queues = RabbitConfig.QUEUE_N)
    public void receive(Message message, Channel channel) throws Exception {
        String msg=new String(message.getBody(),"UTF-8");
        log.info("当前时间:{}，收到队列消息:{}", DateUtil.now(),msg);
        //后续操作
        Set<String> userSet = socketChannelPool.getBindUserMap().keySet();
        //筛去自己
        /*List<String> userList = userSet.stream().filter(i -> !msg.equals(i)).collect(Collectors.toList());
        for(String userId:userList){
            socketChannelPool.sendToUser(userId,WsBean.get(WsBean.CallBackEm.notice));
        }*/
        for(String userId:userSet){
            socketChannelPool.sendToUser(userId,WsBean.get(WsBean.CallBackEm.notice));
        }

    }
}
